﻿<Page
    RequestedTheme="Light"
    x:Class="wToggl.HubPage"
    Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wToggl="using:wToggl" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:wToggle.Platform.Data"
    mc:Ignorable="d">
    <Page.Resources>
        <ResourceDictionary>
            <wToggl:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
            <DataTemplate x:Key="HubSectionHeaderTemplate">
                <TextBlock Margin="0,0,0,-9.5" Text="{Binding}"/>
            </DataTemplate>
        </ResourceDictionary>
    </Page.Resources>
    <Page.BottomAppBar>
        
        <CommandBar ClosedDisplayMode="Compact">
            <CommandBar.PrimaryCommands>
                <AppBarButton Icon="Clock" Visibility="Visible" IsEnabled="True" Label="Reports" Name="ReportsButton" Click="ReportsButton_OnClick" />
                <AppBarButton Icon="Sync" Click="ReloadWorkspaceButton_OnClick" Visibility="Visible" IsEnabled="True" Label="WS Sync" Name="ReloadWorkspaceButton" />   
            </CommandBar.PrimaryCommands>
            <CommandBar.SecondaryCommands>
                <AppBarButton Click="EditWorkspaceButton_OnClick" Visibility="Visible" IsEnabled="True" Label="Workspace setings" Name="EditWorkspaceButton" />
                <AppBarButton Visibility="Visible" IsEnabled="True" Label="Switch workspace" Name="SwitchWorkspaceButton" Click="SwitchWorkspaceButton_OnClick">
                    <FlyoutBase.AttachedFlyout>
                        <Flyout Placement="Full">
                            <Grid>
                                <ListView Margin="10,0" ShowsScrollingPlaceholders="True" SelectionMode="Single" SelectionChanged="WorkspaceList_OnSelectionChanged" Name="WorkspaceList" ItemsSource="{Binding Workspaces}" VerticalAlignment="Center">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Margin="6" Text="{Binding Name}" FontSize="30"/>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </Grid>
                        </Flyout>
                    </FlyoutBase.AttachedFlyout>
                </AppBarButton>
                <AppBarButton Label="Log off" Name="LogOffButton" Click="LogOffButton_OnClick"/>
                <AppBarButton Label="About" Name="AboutButton" Click="AboutButton_OnClick"/>
            </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>
    
    <Grid x:Name="LayoutRoot">
        <wToggl:BackgroundWorkOverlay Canvas.ZIndex="10" IsVisible="{Binding IsWorking}" OverlayMessage="{Binding OverlayMessage}"/>
        <Hub x:Name="Hub" Header="toggly">
            <HubSection  x:Uid="HubSection1" Header="LIVE TRACKING" HeaderTemplate="{StaticResource HubSectionHeaderTemplate}">
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBox IsSpellCheckEnabled="False" PlaceholderText="What are you working on?" Text="{Binding TimeEntryDescription, Mode=TwoWay}"/>
                        
                        <wToggl:TagSelector Header="Project" IsMultiSelect="False" Grid.Row="1" PlaceholderText="Select a project" Items="{Binding Workspace.Projects}" SelectedItems="{Binding SelectedProjects, Mode=TwoWay}">
                            <wToggl:TagSelector.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}"/>
                                </DataTemplate>
                            </wToggl:TagSelector.ItemTemplate>
                        </wToggl:TagSelector>

                        <wToggl:TagSelector Header="Tags" PlaceholderText="Select tags" Grid.Row="2" Items="{Binding Workspace.Tags}" SelectedItems="{Binding SelectedTags, Mode=TwoWay}">
                            <wToggl:TagSelector.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}"/>
                                </DataTemplate>
                            </wToggl:TagSelector.ItemTemplate>
                        </wToggl:TagSelector>
                        <Grid  Grid.Row="3" Visibility="{Binding IsChessRunning, ConverterParameter=not, Converter={StaticResource BoolToVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Button Visibility="{Binding StartVisibility}" Click="StartButton_OnClick" Style="{StaticResource PrimaryButton}" Width="180" Height="90" HorizontalAlignment="Center" Content="Go!"/>
                            
                            <Grid Visibility="{Binding StopVisibility}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Button Grid.Column="1" Click="StopButton_OnClick" Name="StopButton" Style="{StaticResource StopButton}" Width="180" Height="90" HorizontalAlignment="Center" Content="Stop"/>

                                <HyperlinkButton Click="DiscardButton_OnClick" Padding="0" Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="Black" Name="DiscardButton" HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <StackPanel Height="35" Margin="0" Orientation="Horizontal">
                                        
                                        <SymbolIcon Margin="-8" Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Symbol="Cancel"/>
                                        <TextBlock Margin="0" Text="Discard" FontSize="15" VerticalAlignment="Center"/>
                                    </StackPanel>
                                    
                                </HyperlinkButton>

                            </Grid>
                           
                            <TextBlock Grid.Row="1" HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding TrackingDuration}" FontSize="42"/>
                            <Grid Grid.Row="2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <TextBlock Style="{StaticResource BodyTextBlockStyle}" HorizontalAlignment="Center" Text="or"/>
                                <HyperlinkButton Name="ManualTimeButton" Click="ManualTimeButton_OnClick" FontSize="20" Grid.Row="1" FontWeight="Bold" FontStretch="Expanded" Foreground="{StaticResource PhoneAccentBrush}" HorizontalAlignment="Center" Content="USE MANUAL MODE"/>
                            </Grid>
                        </Grid>
                    </Grid> 
                </DataTemplate>
            </HubSection>

            <HubSection x:Uid="HubSection2" Header="RECENT ENTRIES" Width="Auto" HeaderTemplate="{StaticResource HubSectionHeaderTemplate}">
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch">
                        <ProgressRing Visibility="{Binding RecentEntryProgressCircleVisibility}" IsActive="{Binding IsLoadingEntries}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" Margin="0,0,0,20">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock FontSize="22" FontWeight="Bold" HorizontalAlignment="Stretch" Style="{StaticResource BodyTextBlockStyle}" Text="Last 72 hours:"/>
                                <TextBlock FontSize="22" HorizontalAlignment="Right" Grid.Column="1" Style="{StaticResource BodyTextBlockStyle}" Foreground="{StaticResource PhoneAccentBrush}" FontWeight="Bold" Text="{Binding WorkTotal}"/>
                            </Grid>
                            <ListView SelectionMode="Single" Grid.Row="1" Visibility="{Binding RecentEntryListVisibility}" ItemsSource="{Binding RecentEntries}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
                                <ListView.ItemContainerStyle>
                                    <Style TargetType="ListViewItem">
                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                    </Style>
                                </ListView.ItemContainerStyle>
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Tapped="TimeEntryGrid_OnTapped" Holding="TimeEntryGrid_OnHolding" IsTapEnabled="True" Name="TimeEntryGrid"  Margin="0,5" d:DataContext="{d:DesignInstance data:TimeEntry}" HorizontalAlignment="Stretch">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <FlyoutBase.AttachedFlyout>
                                                <MenuFlyout Placement="Bottom">
                                                    <MenuFlyoutItem Text="Resume" Name="ResumeEntryMenu" Click="ResumeEntryMenu_OnClick" />
                                                    <MenuFlyoutItem Text="Show details" Name="DetailsEntryMenu"  Click="DetailsEntryMenu_OnClick" />
                                                    <MenuFlyoutItem Text="Delete" Name="DeleteEntryMenu" Click="DeleteEntryMenu_OnClick"/>
                                                </MenuFlyout>
                                            </FlyoutBase.AttachedFlyout>
                                            <TextBlock MaxWidth="230" TextTrimming="CharacterEllipsis" FontWeight="Bold" Grid.Row="0" HorizontalAlignment="Stretch" Style="{StaticResource BodyTextBlockStyle}" Text="{Binding ReadableDescription}"/>
                                            <TextBlock Grid.Row="0" FontWeight="Bold" HorizontalAlignment="Right" Style="{StaticResource BodyTextBlockStyle}" Grid.Column="1" Text="{Binding DurationString}"/>
                                            <TextBlock Grid.Row="1" Style="{StaticResource TimeEntrySubitem}" Grid.Column="0" Text="{Binding Project.Name}" HorizontalAlignment="Stretch" />
                                            <TextBlock MaxWidth="100" TextTrimming="CharacterEllipsis" Grid.Row="1" Style="{StaticResource TimeEntrySubitem}" Grid.Column="1" Text="{Binding TagString}" HorizontalAlignment="Right" TextWrapping="WrapWholeWords"/>
                                        </Grid>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </HubSection>
            
            <HubSection x:Uid="HubSection4" Header="CHESS TRACKER" HeaderTemplate="{StaticResource HubSectionHeaderTemplate}">
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding ChessTrackingDuration}" FontSize="42"/>

                            <HyperlinkButton Visibility="{Binding IsChessRunning, Converter={StaticResource BoolToVisibilityConverter}}" Click="DiscardChessButton_OnClick" Padding="0" Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="Black" Name="DiscardButton" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <StackPanel Height="35" Margin="0" Orientation="Horizontal">
                                    <SymbolIcon Margin="-8" Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Symbol="Cancel"/>
                                    <TextBlock Margin="0" Text="Discard" FontSize="15" VerticalAlignment="Center"/>
                                </StackPanel>

                            </HyperlinkButton>
                        </Grid>                    
                        <ScrollViewer Grid.Row="1">
                            <ItemsControl ItemsSource="{Binding Projects}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <ToggleSwitch Toggled="ChessSwitch_OnToggled" Name="ChessSwitch" HorizontalAlignment="Stretch" OnContent="Timing..." OffContent="Stopped" Header="{Binding Name}">
                                                <ToggleSwitch.OnContentTemplate>
                                                    <DataTemplate>
                                                        <TextBlock FontSize="20" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding}"/>
                                                    </DataTemplate>
                                                </ToggleSwitch.OnContentTemplate>
                                                <ToggleSwitch.OffContentTemplate>
                                                    <DataTemplate>
                                                        <TextBlock FontSize="20" Text="{Binding}"/>
                                                    </DataTemplate>
                                                </ToggleSwitch.OffContentTemplate>
                                                <ToggleSwitch.HeaderTemplate>
                                                    <DataTemplate>
                                                        <TextBlock FontSize="20" Text="{Binding}" Style="{StaticResource BodyTextBlockStyle}" FontWeight="Bold"/>
                                                    </DataTemplate>
                                                </ToggleSwitch.HeaderTemplate>
                                            </ToggleSwitch>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </Grid>                
                </DataTemplate>
            </HubSection>
        </Hub>   
    </Grid>
</Page>